/**
 * @Description Ext.translate for ExtJS 2.x and 3.x; Auto translation with Google Translation API (java/php server side)
 * @author  Tomislav Milkovic
 * @license LGPLv3 http://www.opensource.org/licenses/lgpl-3.0.html
 * @version 1.6, 20.09.2009.
 */


/**
 * Ext.MessageBox override to add automatic translation on message boxes.
 * Only this script must be added to HTML  page. No need for additional coding. 
 */
Ext.namespace('Ext.translate.MessageBox');
Ext.translate.MessageBox.show = Ext.MessageBox.show;
Ext.translate.MessageBox.updateProgress = Ext.MessageBox.updateProgress;
Ext.translate.MessageBox.updateText = Ext.MessageBox.updateText;
Ext.MessageBox.translated = true;
Ext.MessageBox.translateFields = ['msg','title','progressText'];
Ext.MessageBox.translateStore = {};

Ext.MessageBox.updateProgress = function(value, progressText, msg){

	if (this.translated || (!defined(progressText)) || (progressText.trim().length==0) || (Ext.translate.currentLanguage == Ext.translate.oldLanguage) || !Ext.translate.Manager.isLanguageSet()){
		Ext.translate.MessageBox.updateProgress.call(Ext.MessageBox,value, progressText, msg);
		return;
	};
	
	
    var callbackFn = function(obj,value,msg) {
    	
        if (obj[Ext.translate.currentLanguage]==undefined){
        	Ext.translate.MessageBox.updateProgress.call(Ext.MessageBox,value, obj[Ext.translate.defaultLanguage], msg);    	
            return;
         } ;

         Ext.translate.MessageBox.updateProgress.call(Ext.MessageBox,value, obj[Ext.translate.currentLanguage], msg);    	
    };

    var obj = {'value' : progressText, 'field' : this.translateFields[0]};
    this.translateStore[obj.field] = Ext.translate.Cache.setTranslation(obj);
    
	var cacheobj = Ext.translate.Cache.getTranslationByID(this.translateStore[this.translateFields[0]]);
	var callback = {'callback' : callbackFn,
			scope : this,
			params : [value,msg]};		
	Ext.translate.Cache.translate(cacheobj,callback);  	
	
  
    };

Ext.MessageBox.updateText = function(text){

		if (this.translated || (!defined(text)) || (text.trim().length==0) || (Ext.translate.currentLanguage == Ext.translate.oldLanguage) || !Ext.translate.Manager.isLanguageSet()){
			Ext.translate.MessageBox.updateText.call(Ext.MessageBox,text);
			return;
		}

        var callbackFn = function(obj) {
        	
            if (obj[Ext.translate.currentLanguage]==undefined){
            	
            	Ext.translate.MessageBox.updateText.call(Ext.MessageBox, obj[Ext.translate.defaultLanguage]);
                return;
             } ;

             Ext.translate.MessageBox.updateText.call(Ext.MessageBox,obj[Ext.translate.currentLanguage]);
        };

        var obj = {'value' : text, 'field' : this.translateFields[0]};
        this.translateStore[obj.field] = Ext.translate.Cache.setTranslation(obj);
        
    	var cacheobj = Ext.translate.Cache.getTranslationByID(this.translateStore[this.translateFields[0]]);
		var callback = {'callback' : callbackFn,
				scope : this,
				params : [cacheobj]};		
		Ext.translate.Cache.translate(cacheobj,callback);    	

		
    };

Ext.MessageBox.show = function(options){

	Ext.MessageBox.translated = true;
	
	
	var fn = function(item, index, allItems){
        var obj = {'value' : this[item] || '', 'field' : item};
        Ext.MessageBox.translateStore[item] = Ext.translate.Cache.setTranslation(obj);        
	};
	Ext.each(this.translateFields, fn, options);	
	
	
	
    var callbackFn = function (obj,index,options){

        options[Ext.MessageBox.translateFields[index]] = obj[Ext.translate.currentLanguage];      
        var idx = index + 1;
                        
        if (idx == Ext.MessageBox.translateFields.length){
                Ext.translate.MessageBox.show.call(Ext.MessageBox,options);
                Ext.MessageBox.translated = false;
                return;
        };
        
        var cacheobj = Ext.translate.Cache.getTranslationByID(this.translateStore[this.translateFields[idx]]);
        var callback = {'callback' : callbackFn,
                        scope : this,
                        params : [idx,options]};                
        Ext.translate.Cache.translate(cacheobj,callback);
    };

	var callback = {'callback' : callbackFn, scope : this, params : [0,options]};	
	var cacheobj = Ext.translate.Cache.getTranslationByID(this.translateStore[this.translateFields[0]]);
	
	if (!Ext.translate.Cache.translate(cacheobj,callback)){
	        Ext.translate.MessageBox.show.call(Ext.MessageBox,options);
	        Ext.MessageBox.translated = false;
	}
}; 
